/**
  ******************************************************************************
寮曡剼 		APB2 鎬荤嚎 					|APB1 鎬荤嚎
		USART1 		|	USART2 	USART3 	UART4 	UART5
TX 		PA9 		|	PA2 	PB10 	PC10 	PC12
RX 		PA10 		|	PA3 	PB11 	PC11 	PD2
SCLK	PA8 		|	PA4 	PB12
nCTS 	PA11		|	PA0 	PB13
nRTS 	PA12 		|	PA1 	PB14

  ******************************************************************************

  ******************************************************************************
  */
  
#include "bsp_usart1.h"

#include "main.h"

 /**
  * @brief  USART1 GPIO 配置,工作模式配置。115200 8-N-1
  * @param  无
  * @retval 无
  */

 void NVIC_Configuration_1(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	/* 配置 NVIC 为优先级组 1 */
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	/* 配置中断源：按键 1 */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	/* 配置抢占优先级： 1 */
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	/* 配置子优先级： 1 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	/* 使能中断通道 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void USART1_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* config USART1 clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
	
	/* USART1 GPIO config */
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);    
	
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* USART1 mode config */
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);
	NVIC_Configuration_1();
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

	USART_Cmd(USART1, ENABLE);
}




 void NVIC_Configuration_2(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	/* 配置 NVIC 为优先级组 1 */
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	/* 配置中断源：按键 1 */
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	/* 配置抢占优先级： 1 */
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	/* 配置子优先级： 1 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	/* 使能中断通道 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void USART2_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* config USART1 clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	/* USART1 GPIO config */
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);    
	
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* USART1 mode config */
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART2, &USART_InitStructure);
	// 串口中断优先级配置
	NVIC_Configuration_2();
	// 配置完 NVIC 之后调用 USART_ITConfig 函数使能 USART 接收中断。
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

	USART_Cmd(USART2, ENABLE);
}

void USART_Config(void)
{
	USART1_Config();
	USART2_Config();

}

char get_usart2_buf[4];

void gRxHostBufferFlush(u32 BufferSize,char *buf){
    for(u32 counter=0; counter<BufferSize; counter++) buf[counter] = 0;
}


char get_usart2_i=0;
bool get_usart2_buffer = FALSE;

void USART2_IRQHandler_FUN(void)
{

	u8 data;

	(void)USART2->SR;   //Error clear;
	data =	(u8)(USART2->DR & (u16)0x01FF);
	USART_ClearITPendingBit(USART2, USART_IT_RXNE);
	get_usart2_buffer = FALSE;


	get_usart2_buf[get_usart2_i] = data;
	USART_SendData(USART1, get_usart2_buf[get_usart2_i]);	
	get_usart2_i++;
	if(get_usart2_buf[0] != 0xFF)
	{
		get_usart2_i=0;
	}
		
	if(get_usart2_i==4)
	{	get_usart2_buffer = TRUE;
		AnalizingBuf();
		get_usart2_i = 0;
		gRxHostBufferFlush(4,get_usart2_buf);
	}
	

}

void AnalizingBuf(void)
{
	if(get_usart2_buffer == FALSE)
		return;

	switch (get_usart2_buf[2])
	{
		case 1://上
			switch (get_usart2_buf[3])
			{
				case 1://按下
					printf("forward down\n");
					break;
				case 0://按抬起
					printf("forward up\n");
					break;
               default :
					break;
			}
			break;

		case 2://下
			switch (get_usart2_buf[3])
			{
				case 1://按下
					printf("back on\n");
					break;
				case 0://按抬起
					printf("back off\n");
					break;
				default :
					break;
			}
			break;
		case 3://中间
			switch (get_usart2_buf[3])
			{
				case 1://按下
					printf("centre on\n");
					break;
				case 0://按抬起
					printf("centre off\n");
					break;
				default :
					break;
			}
			break;
		case 4://左
			switch (get_usart2_buf[3])
			{
				case 1://按下
					printf("left on\n");
					break;
				case 0://按抬起
					printf("left off \n");
					break;
				default :
					break;
			}
			break;
		case 5://右
			switch (get_usart2_buf[3])
			{
				case 1://按下
					printf("right on \n");
					break;
				case 0://按抬起
					printf("right off\n");
					break;
				default :
					break;
			}
			break;

		default :
			break;			
	}


}

char get_buf_usart1[4];
char get_usart1_i=0;
void USART1_IRQHandler_FUN(void)
{

	u8 data;

	(void)USART2->SR;   //Error clear;
	data =	(u8)(USART2->DR & (u16)0x01FF);
	USART_ClearITPendingBit(USART2, USART_IT_RXNE);
	


	get_buf_usart1[get_usart1_i] = data;
	USART_SendData(USART1, get_buf_usart1[get_usart1_i]);	
	get_usart1_i++;
	if(get_buf_usart1[0] != 0xFF)
	{
		get_usart1_i=0;
	}
		
	if(get_usart1_i==4)
	{	
		AnalizingBuf2();
		get_usart1_i = 0;
		gRxHostBufferFlush(4,get_buf_usart1);
	}
	

}

void AnalizingBuf2(void)
{
//	if(get_buf_buffer == FALSE)
//		return;

	switch (get_buf_usart1[2])
	{
		case 1://上
			switch (get_buf_usart1[3])
			{
				case 1://按下
					printf("forward down\n");
					break;
				case 0://按抬起
					printf("forward up\n");
					break;
               default :
					break;
			}
			break;

		case 2://下
			switch (get_buf_usart1[3])
			{
				case 1://按下
					printf("back on\n");
					break;
				case 0://按抬起
					printf("back off\n");
					break;
				default :
					break;
			}
			break;
		case 3://中间
			switch (get_buf_usart1[3])
			{
				case 1://按下
					printf("centre on\n");
					break;
				case 0://按抬起
					printf("centre off\n");
					break;
				default :
					break;
			}
			break;
		case 4://左
			switch (get_buf_usart1[3])
			{
				case 1://按下
					printf("left on\n");
					break;
				case 0://按抬起
					printf("left off \n");
					break;
				default :
					break;
			}
			break;
		case 5://右
			switch (get_buf_usart1[3])
			{
				case 1://按下
					printf("right on \n");
					break;
				case 0://按抬起
					printf("right off\n");
					break;
				default :
					break;
			}
			break;

		default :
			break;			
	}


}




/// 重定向c库函数printf到USART1
int fputc(int ch, FILE *f)
{
		/* 发送一个字节数据到USART1 */
		USART_SendData(USART1, (uint8_t) ch);
		
		/* 等待发送完毕 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);		
	
		return (ch);
}

/// 重定向c库函数scanf到USART1
int fgetc(FILE *f)
{
		/* 等待串口1输入数据 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(USART1);
}
/*********************************************END OF FILE**********************/
